/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package ws;

import dao.AlunoDAO;
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriInfo;
import model.Aluno;

/**
 * REST Web Service
 *
 * @author Hugo
 */
@Path("aluno")
public class AlunoResource {

    @Context
    private UriInfo context;
    private AlunoDAO dao = new AlunoDAO();
    
    public AlunoResource() {        
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List<Aluno> getJson() {
        List<Aluno> alunos = dao.listar();
        return alunos;
    }
    
    @GET
    @Path("/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public Aluno getJson(@PathParam("id") int id) {
        Aluno aluno = dao.buscar(id);
        return aluno;
    }
    
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public int postJson(Aluno aluno) {
        int idNovoAluno = dao.incluir(aluno);
        return idNovoAluno;
    }

    /**
     * PUT method for updating or creating an instance of TesteResource
     * @param content representation for the resource
     * @return an HTTP response with content of the updated or created resource.
     */
    @PUT
    @Consumes(MediaType.APPLICATION_JSON)
    public int putJson(Aluno aluno) {
        int idAlunoAlterado = dao.alterar(aluno);
        return idAlunoAlterado;
    }
    
    @DELETE
    @Path("/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public Aluno deleteJson(@PathParam("id") String id) {
        Aluno alunoRemovido = dao.excluir(Integer.parseInt(id));
        return alunoRemovido;
    }
}
